//
//  TechnologyView.m
//  HomePage
//
//  Created by xiong qi on 13-5-31.
//  Copyright (c) 2013年 winston. All rights reserved.
//

#import "TechnologyView.h"
#import "../../ResourceLoader/ResourceLoader/ResourceLoader.h"
#import "../../ResourceLoader/ResourceLoader/JSONKit.h"
#import "StoreItemView.h"

#define  TemplateWidth                  225
#define  TemplateHeight                 195
#define  TemplateSpaceX                 30
#define  TemplateSpaceY                 20
#define  TemplateTopSpace               50

@implementation TechnologyView
@synthesize dataSource,rootPath;

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        // Initialization code
        [self setBackgroundColor:[UIColor clearColor]];
        
        int width = (TemplateSpaceX+TemplateWidth)*3;
        int height = (TemplateSpaceY+TemplateHeight)*2+TemplateTopSpace;
        
        contentView = [[UIScrollView alloc] initWithFrame:CGRectMake((frame.size.width-width)/2, 50, width, height)];
        [self addSubview:contentView];
        [contentView release];
        contentView.pagingEnabled = YES;
        contentView.showsHorizontalScrollIndicator = NO;
        contentView.showsVerticalScrollIndicator = NO;
        
        [self ReloadView];
    }
    return self;
}

-(void)ReloadView
{
    int page = 0;
    int row = 0;
    int col = 0;
    
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString * localpath= [NSString stringWithFormat:@"%@/paddata/company/Technology",[paths objectAtIndex:0]];
    self.rootPath = localpath;
    NSString * localfilepath = [NSString stringWithFormat:@"%@/Technology.tpl",localpath];
    NSData * data = [NSData dataWithContentsOfFile:localfilepath];
    self.dataSource = [data objectFromJSONData];
    
    int count = [dataSource count];
    
    contentView.contentSize = CGSizeMake(((count-1)/6+1)*contentView.frame.size.width,contentView.frame.size.height);
    
    int centerX = 0;
    int centerY = 0;
    for (int i=0; i<count; i++) {
        
        //一面放6个，放不下的放到另一面
        page = i/6;
        row = (i%6)/3;
        col = (i%6)%3;
        
        StoreItemView * itemview = [[StoreItemView alloc] initWithFrame:CGRectMake(0, 0, TemplateWidth, TemplateHeight)];
        itemview.tag = 100+i;
        itemview.delegate = self;
        [contentView addSubview:itemview];
        [itemview release];
        
        centerX = page * contentView.frame.size.width + TemplateSpaceX + col*(TemplateWidth+TemplateSpaceX)+TemplateWidth/2;
        centerY = row * (TemplateHeight+TemplateSpaceY)+TemplateTopSpace+TemplateHeight/2;
        
        itemview.center = CGPointMake(centerX, centerY);
        
        NSDictionary * dic = [dataSource objectAtIndex:i];
        NSString * title = [dic objectForKey:@"Title"];
        NSString * imagepath = [NSString stringWithFormat:@"%@/%@",localpath,[dic objectForKey:@"ImageName"]];
        
        itemview.dataSource = dic;
        [itemview LoadTitle:title ImagePath:imagepath];
    }
}

-(void)ItemBeTouch:(StoreItemView *)itemview
{
    [[NSNotificationCenter defaultCenter]postNotificationName:Notification_Navigate_To_Technology object:itemview.dataSource userInfo:nil];
}

-(void)dealloc
{
    self.dataSource = nil;
    self.rootPath = nil;
    [super dealloc];
}
@end
